package fun.sboy.panel;

import fun.sboy.api.API;
import fun.sboy.pojo.ManagerUser;
import fun.sboy.pojo.Trade;
import fun.sboy.pojo.TradeVO;
import fun.sboy.util.JDBCUtil;
import fun.sboy.view.HomeView;

import javax.swing.*;
import java.awt.*;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderListPanel extends BaseJPanel {

    private int width = 1000;
    private int height = 800;
    private List<TradeVO> dataList = null;

    public OrderListPanel(LayoutManager layout, boolean isDoubleBuffered) {
        super(layout, isDoubleBuffered);
        initialization();
    }

    public OrderListPanel(LayoutManager layout) {
        super(layout);
        initialization();
    }

    public OrderListPanel(boolean isDoubleBuffered) {
        super(isDoubleBuffered);
        initialization();
    }

    public OrderListPanel() {
        initialization();
    }

    @Override
    public void initialization() {
    }

    @Override
    public void reload() {
        ManagerUser user = HomeView.user;

        //清除集合中的所有元素
        System.gc();
        removeAll();

        String[] titles = {"支入/支出", "金额", "用户姓名", "卡号", "交易时间"};
        JPanel titlePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 30, 5));
        titlePanel.setBounds(20, 20, 800, 30);
        for (int i = 0; i < titles.length; i++) {
            String title = titles[i];
            JLabel text = new JLabel(title);
            text.setForeground(Color.white);
            if (i < 2) {
                text.setPreferredSize(new Dimension(60, 20));
            } else {
                text.setPreferredSize(new Dimension(150, 20));
            }
            titlePanel.add(text);
        }
        titlePanel.setBackground(Color.darkGray);
        add(titlePanel);

        //添加一个浏览
        dataList = API.listTrade();

        int x = 20, y = 55;
        for (TradeVO tradeVO : dataList) {
            JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 30, 5));
            panel.setBounds(x, y, 800, 30);

            //信息控件
            Field[] fields = TradeVO.class.getDeclaredFields();
            int num = 0;
            for (Field field : fields) {
                field.setAccessible(true);
                try {
                    JLabel text = new JLabel();
                    text.setText(String.valueOf(field.get(tradeVO)));
                    if (num < 2) {
                        text.setPreferredSize(new Dimension(60, 20));
                    } else {
                        text.setPreferredSize(new Dimension(150, 20));
                    }
                    text.setForeground(Color.white);
                    panel.add(text);
                    num++;
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
            }

            //设置背景颜色
            if (tradeVO.getPayeeOrRemitter().equals("收入")) {
                panel.setBackground(new Color(46, 204, 113));
            } else {
                panel.setBackground(new Color(230, 126, 34));
            }

            add(panel);
            y += 32;
        }

        if (dataList.size() == 0 || dataList == null) {
            JPanel panel = new JPanel();
            panel.setBackground(Color.lightGray);
            JLabel jLabel = new JLabel("\n\n没有交易数据哦！！！");
            jLabel.setForeground(Color.white);
            panel.add(jLabel);
            panel.setBounds(x,y,800,100);
            add(panel);
        }
    }
}
